package com.luoy.threads;

import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;

public class TestThread {

    public static void main(String[] args) {

        //方式一:  实现runnable接口   run方法
        MyRunnable myRunnable = new MyRunnable();
        Thread thread = new Thread(myRunnable);
        thread.start();
        new Thread(new Runnable() {
            public void run() {
                System.out.println("线程名称:　" + Thread.currentThread().getName());
            }
        }).start();


        //方式二:  extends Thread类
        MyThread myThread = new MyThread();
        myThread.start();

        //方式三
        Callable<Integer> callable = new MyCallable();
        //1.执行 Callable 方式，需要 FutureTask 实现类的支持，用于接收运算结果。
        FutureTask<Integer> result = new FutureTask<>(callable);
        new Thread(result).start();
        //2.接收线程运算后的结果
        try {
            //FutureTask 可用于 闭锁 类似于CountDownLatch的作用，在所有的线程没有执行完成之后这里是不会执行的
            Integer sum = result.get();
            System.out.println(sum);
            System.out.println("------------------------------------");
        } catch (InterruptedException | ExecutionException e) {
            e.printStackTrace();
        }
    }
}
